在 Elixir 中,資料處理遵循兩種截然不同的哲學: 貪婪 (積極主動)以及 懶惰。理解這兩者的取捨對記憶體效率和系統穩定性至關重要。
1. 可遍歷協議
技術上來說,可以被迭代的物件被稱為實作了 可遍歷協議。這個通用契約允許各種資料結構使用相同的函數集。
2. 貪婪模組與懶惰模組
其中 Enum 模組是 貪婪。它可能立即消耗整個集合的所有內容,在每一個管道步驟中創建中間列表。相反地, Stream 模組是 懶惰。下一個值僅在需要時才會計算 僅 才會被計算。
3. 規格與結果之別
一個 Stream 值 是我們意圖的規格,而非實際結果。Streams 具備可遍歷性和可組合性,讓您能層疊轉換操作,直到將 Stream 傳遞給「積極」的終點(如 Enum.to_list/1.
4. 理念純粹性
混合不同範型(函數式與物件導向)會削弱函數式方法帶來的好處。建議以宣告式轉換取代命令式迴圈,以提升可預測性。
main.py
TERMINALbash — 80x24
> Ready. Click "Run" to execute.
>
QUESTION 1
What happens when you pass a collection to the
Enum module?It creates a lazy recipe for later execution.
It potentially consumes all contents immediately and returns a result.
It returns a Stream struct containing the function logic.
It converts the data into an object-oriented class.
✅ Correct!
Correct! Enum is greedy, meaning it realizes the entire result immediately.❌ Incorrect
That describes the Stream module. Enum is 'greedy' and processes data immediately.QUESTION 2
Which statement best describes a 'Stream' in Elixir?
A realized list of integers.
A specification of intended work, not the result itself.
A faster version of Enum for small lists.
A specific type of Map used for key-value storage.
✅ Correct!
Exactly. Streams are recipes or specifications that define work to be done later.❌ Incorrect
Streams do not hold results; they hold the logic to generate results on demand.QUESTION 3
Why would you choose Stream over Enum for a 10GB text file?
Enum cannot read files.
Stream executes faster for small datasets.
Stream processes data line-by-line, preventing memory exhaustion.
Stream automatically sorts the file contents.
✅ Correct!
Yes! Lazy processing avoids loading the whole file into RAM at once.❌ Incorrect
Enum would attempt to load the entire 10GB into memory, likely crashing the VM.QUESTION 4
According to the lesson logic, what is a danger of mixing functional and OO paradigms?
It makes the code run significantly faster.
It dilutes the benefits of the functional approach.
It is required for the Enumerable protocol.
It allows Elixir to run on the JVM.
✅ Correct!
Right. Paradigmatic purity helps maintain clarity and functional predictability.❌ Incorrect
Actually, the text states it dilutes the benefits of functional programming.QUESTION 5
What is the requirement for a data type to be usable with Enum and Stream modules?
It must be a binary string.
It must implement the Enumerable protocol.
It must be smaller than 1MB.
It must be a recursive Keyword list.
✅ Correct!
Correct! The Enumerable protocol is the universal interface for collections.❌ Incorrect
Any structure implementing the Enumerable protocol can be used.Module Challenge: Prime Sequences & String Cleaning
Applying greedy and lazy transformations to complex data tasks.
You are tasked with generating mathematical sequences and cleaning messy user input. You must use list comprehensions and high-level string functions to achieve efficient results.
Q
1. Using your previously written 'span' function (which returns a list from 2 to n), use list comprehensions to return a list of the prime numbers from 2 to n.
Solution:
Model Solution: elixir def primes_up_to(n) do range = span(2, n) for x <- range, is_prime?(x), do: x end defp is_prime?(2), do: true defp is_prime?(n) when n < 2, do: false defp is_prime?(n) do Enum.all?(2..round(:math.sqrt(n)), fn x -> rem(n, x) != 0 end) end This uses the span function as a generator and a filter predicate to identify primes.
Model Solution: elixir def primes_up_to(n) do range = span(2, n) for x <- range, is_prime?(x), do: x end defp is_prime?(2), do: true defp is_prime?(n) when n < 2, do: false defp is_prime?(n) do Enum.all?(2..round(:math.sqrt(n)), fn x -> rem(n, x) != 0 end) end This uses the span function as a generator and a filter predicate to identify primes.
Q
2. [Writing Task] Write a function to capitalize the sentences in a string. Each sentence is terminated by a period and a space ('. '). Assume 150-word output capacity for the logic explanation.
Solution:
To capitalize sentences in a string where casing is currently random, follow these steps (approx 150 words of logic): First, use `String.split(input, ". ")` to break the string into a list of individual sentences. Note that the delimiter is lost in the split. Second, apply a transformation function to each element using `Enum.map`. Inside this map, use `String.capitalize/1`, which lowercases the whole string and then capitalizes the very first character. This ensures 'tHe dOg.' becomes 'The dog.'. Finally, use `Enum.join(". ")` to re-attach the sentences with the proper punctuation. Example code: elixir def capitalize_sentences(str) do str |> String.split(". ") |> Enum.map(&String.capitalize/1) |> Enum.join(". ") end
To capitalize sentences in a string where casing is currently random, follow these steps (approx 150 words of logic): First, use `String.split(input, ". ")` to break the string into a list of individual sentences. Note that the delimiter is lost in the split. Second, apply a transformation function to each element using `Enum.map`. Inside this map, use `String.capitalize/1`, which lowercases the whole string and then capitalizes the very first character. This ensures 'tHe dOg.' becomes 'The dog.'. Finally, use `Enum.join(". ")` to re-attach the sentences with the proper punctuation. Example code: elixir def capitalize_sentences(str) do str |> String.split(". ") |> Enum.map(&String.capitalize/1) |> Enum.join(". ") end
Q
3. What occurs when you modify a printable character function example to use '99.0' (a float) where a charlist is expected?
Solution:
In Elixir, a charlist is a list of integers. If you provide '99.0', the system will raise a FunctionClauseError or a type error because '99.0' is a floating-point number, not an integer representing a codepoint. Charlist processing functions rely on the integer values (like 99 for 'c') to determine printability; floats do not satisfy the Enumerable protocol for lists or the bitstring requirements for binaries in this context.
In Elixir, a charlist is a list of integers. If you provide '99.0', the system will raise a FunctionClauseError or a type error because '99.0' is a floating-point number, not an integer representing a codepoint. Charlist processing functions rely on the integer values (like 99 for 'c') to determine printability; floats do not satisfy the Enumerable protocol for lists or the bitstring requirements for binaries in this context.